package com.hk.yajin.function.videos.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.base.SingleItemListFragment;
import com.hk.yajin.function.ContainerActivity;
import com.hk.yajin.function.videos.activity.VideoDetailActivity;
import com.hk.yajin.function.videos.adapter.MyseriseListQuickAdapter;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.DataListModel;
import com.hk.yajin.model.Series;
import com.hk.yajin.model.Video;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import org.xutils.common.util.LogUtil;

import java.util.Map;

/**
 * Created by 伦哥哥 on 2017/6/25.
 */

public class MySeriseVideoListFragment extends SingleItemListFragment<Series> {

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        super.initView(inflater, container);
        return mContentView;
    }


    @Override
    public void initAdapter() {
        mQuickAdapter = new MyseriseListQuickAdapter(dataList, getActivity());
        mQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Intent videoIntent = new Intent(getActivity(), ContainerActivity.class);
                videoIntent.putExtra(ContainerActivity.ID,dataList.get(position).getS_id());
                videoIntent.putExtra(ContainerActivity.FRAGMENT_TAG,"s_id");
                startActivity(videoIntent);
            }
        });
        super.initAdapter();
    }


    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(manager);
    }

    @Override
    public void initDataList() {
        if (loadFinish) {
            loadFinish = false;
            if (page == 1) {
                showLoadingView();
            }
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("page", String.valueOf(page));
            reqParam.put("per_page", String.valueOf(pageSize));


            HttpRequestUtils.post(true, UrlConstants.FUNCTION_MY_SERIER_LIST, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    if (swipe_container != null && swipe_container.isRefreshing()) {
                        dataList.clear();
                    }
                    swipe_container.setRefreshing(false);
                    LogUtil.d("成功：" + data);
                    if (!TextUtils.isEmpty(data)) {
                        DataListModel<Series> model = FastjsonUtils.getBeanObject(data, new TypeReference<DataListModel<Series>>() {
                        });
                        handleData(model.getData());
                    } else {
                        mQuickAdapter.loadMoreFail();
                        mQuickAdapter.notifyDataSetChanged();
                        loadFinish = true;
                    }
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    LogUtil.d("请求异常：" + tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    LogUtil.d("登录失败：" + bean.getMsg());
                }
            });
        }
    }


}
